#include "stdafx.h"
#include "SessionMgr.h"

#include "RuleModel.h"
#include "SessionGroup.h"

CSessionMgr* CSessionMgr::s_SessionMgr = nullptr;

CSessionMgr* CSessionMgr::GetInstance()
{
	if( CSessionMgr::s_SessionMgr == nullptr )
	{
		CSessionMgr::s_SessionMgr = new CSessionMgr;
	}

	return CSessionMgr::s_SessionMgr;
}

CSessionMgr::CSessionMgr(void)
{
}

CSessionMgr::~CSessionMgr(void)
{
}

bool CSessionMgr::Initialize( CRuleModel* rule )
{
	CSessionGroup::s_generateSessionID = rule->s_division_capacity * (rule->s_division - 1);

	MAP_SESSION_INFOS::iterator it;
	size_t count = rule->GetSessionGroup( it );
	for( size_t i = 0 ;  i < count ; ++i, ++it )
	{
		int group_id = it->first;
		int session_count = it->second;

		auto group = new CSessionGroup;
		group->Initialize( group_id, session_count );

		m_mapSessionGroups.insert( MAP_SESSION_GROUPS::value_type(group_id,group) );
	}

	return true;
}

void CSessionMgr::Close()
{
	for each( auto value in m_mapSessionGroups )
	{
		auto intance = value.second;
		intance->Close();
	}

	m_mapSessionGroups.clear();
}

void CSessionMgr::LetsRock()
{
	for each( auto group in m_mapSessionGroups )
	{
		auto instance = group.second;
		instance->DoWork();
	}
}
